package com.example.a828502.arraysortrest;

/**
 * 数组排序，比较两个数组中不同的元素
 * Created by 828502 on 2016/5/5.
 */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private ListView mListView, mListView2,mListView3;
    List<String> userNameList = new ArrayList<String>();
    List<Integer> userAgeList = new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.listView);
        mListView2 = (ListView) findViewById(R.id.listView2);
        mListView3 = (ListView) findViewById(R.id.listView3);
        sortArrayCollections();
        sortArrayComparator();
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        list1.add("ss");
        list1.add("aa");
        list1.add("ff");
        list1.add("dsd");

        list2.add("dsd");
        list2.add("34");
        list2.add("ff");
        list2.add("aa");
        compareArray(list1,list2);

    }

    /**
     * 数组元素比较
     * @param list1
     * @param list2
     */
    public void compareArray(List<String> list1,List<String> list2){
        List<String> list3 = new ArrayList<String>();
        //遍历list2数组元素，如果list1不包含此元素，则显示出来
        for (String t : list2) {
            if (!list1.contains(t)) {
                list3.add(t);
            }
        }
        ArrayAdapter adapter
                = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list3);
        mListView.setAdapter(adapter);
    }

    /**
     * 数组元素排序
     */
    public void sortArrayCollections() {
        ArrayList<String> titlesArray = new ArrayList<String>();
        titlesArray.add("43");
        titlesArray.add("25");
        titlesArray.add("17");
        titlesArray.add("34");
        titlesArray.add("59");
        Collections.sort(titlesArray);//数字从小到大排序
        ArrayAdapter adapter
                = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titlesArray);
        mListView3.setAdapter(adapter);
    }
    /**
     * 以数组对象某一元素排序
     */
    public void sortArrayComparator(){
        //创建数组对象
        List<User> userList = new ArrayList<User>();
        userList.add(new User("Lucy", 19));
        userList.add(new User("Jack", 21));
        userList.add(new User("James", 35));
        userList.add(new User("Herry", 19));
        userList.add(new User("Luccy", 48));
        userList.add(new User("James", 18));
        userList.add(new User("Herry", 39));
        Collections.sort(userList, new Comparator<User>() {
            public int compare(User user1, User user2) {
                //先以姓名首字母排序，首字母相同以年龄排序
                int compareName = user1.getName().compareTo(user2.getName());
                if (compareName == 0) {
                    return (user1.getAge() == user2.getAge() ? 0 : (user1.getAge() > user2.getAge() ? 1 : -1));
                }
                return compareName;
                //直接以年龄大小排序
//                return (user1.getAge() == user2.getAge() ? 0 : (user1.getAge() > user2.getAge() ? 1 : -1));
            }
        });
        for (User user : userList) {
            userNameList.add(user.getName()+""+user.getAge());
            userAgeList.add(user.getAge());
        }
        ArrayAdapter adapter
                = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userNameList);
        mListView2.setAdapter(adapter);
    }
}